from django.db import models
from django.contrib.auth.models import User

class Role(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name="角色名称")
    description = models.TextField(blank=True, verbose_name="角色描述")
    
    def __str__(self):
        return self.name
    
    class Meta:
        db_table = 'Role'

class Permission(models.Model):
    code = models.CharField(max_length=50, unique=True, verbose_name="权限代码")
    name = models.CharField(max_length=100, verbose_name="权限名称")
    description = models.TextField(blank=True, verbose_name="权限描述")
    
    def __str__(self):
        return f"{self.name} ({self.code})"
    
    class Meta:
        db_table = 'Permission'

class UserRole(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    
    class Meta:
        db_table = 'User_Role'
        unique_together = ('user', 'role')

class RolePermission(models.Model):
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    permission = models.ForeignKey(Permission, on_delete=models.CASCADE)
    
    class Meta:
        db_table = 'Role_Permission'
        unique_together = ('role', 'permission')

class Goods(models.Model):
    name = models.CharField(max_length=100, verbose_name="商品名称")
    specification = models.CharField(max_length=200, blank=True, verbose_name="规格")
    unit = models.CharField(max_length=20, verbose_name="单位")
    shelf_life = models.IntegerField(null=True, blank=True, verbose_name="保质期（天）")
    min_stock = models.IntegerField(default=0, verbose_name="最小库存")
    max_stock = models.IntegerField(default=1000, verbose_name="最大库存")
    creator = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="创建人")
    
    def __str__(self):
        return self.name
    
    class Meta:
        db_table = 'Goods'

class Stock(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")
    location = models.CharField(max_length=50, verbose_name="货位编号")
    batch_number = models.CharField(max_length=50, verbose_name="批次号")
    quantity = models.IntegerField(verbose_name="数量")
    expiration_date = models.DateField(null=True, blank=True, verbose_name="过期日期")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    
    def __str__(self):
        return f"{self.goods.name} - {self.location}"
    
    class Meta:
        db_table = 'Stock'

class Inbound(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")
    quantity = models.IntegerField(verbose_name="数量")
    supplier = models.CharField(max_length=100, verbose_name="供应商")
    purchase_order = models.CharField(max_length=50, verbose_name="采购单号")
    operator = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="操作员")
    operation_time = models.DateTimeField(auto_now_add=True, verbose_name="操作时间")
    
    def __str__(self):
        return f"{self.goods.name} 入库 {self.quantity}"
    
    class Meta:
        db_table = 'Inbound'

class Outbound(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")
    quantity = models.IntegerField(verbose_name="数量")
    customer = models.CharField(max_length=100, verbose_name="客户")
    approver = models.ForeignKey(User, on_delete=models.CASCADE, related_name='approved_outbounds', verbose_name="审批人")
    operator = models.ForeignKey(User, on_delete=models.CASCADE, related_name='operated_outbounds', verbose_name="操作员")
    operation_time = models.DateTimeField(auto_now_add=True, verbose_name="操作时间")
    
    def __str__(self):
        return f"{self.goods.name} 出库 {self.quantity}"
    
    class Meta:
        db_table = 'Outbound'

class Inventory(models.Model):
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")
    location = models.CharField(max_length=50, verbose_name="货位编号")
    batch_number = models.CharField(max_length=50, verbose_name="批次号")
    system_quantity = models.IntegerField(verbose_name="系统数量")
    actual_quantity = models.IntegerField(verbose_name="实际数量")
    difference = models.IntegerField(verbose_name="差异数量")
    operator = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="操作员")
    operation_time = models.DateTimeField(auto_now_add=True, verbose_name="操作时间")
    note = models.TextField(blank=True, verbose_name="备注")
    
    def __str__(self):
        return f"{self.goods.name} 盘点 {self.operation_time}"
    
    class Meta:
        db_table = 'Inventory'
        verbose_name = '库存盘点'
        verbose_name_plural = '库存盘点'

class FinancialRecord(models.Model):
    RECORD_TYPES = (
        ('income', '收入'),
        ('expense', '支出'),
    )
    record_type = models.CharField(max_length=10, choices=RECORD_TYPES, verbose_name="记录类型")
    amount = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="金额")
    description = models.TextField(blank=True, verbose_name="描述")
    related_id = models.IntegerField(null=True, blank=True, verbose_name="关联ID")
    operator = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="操作员")
    operation_time = models.DateTimeField(auto_now_add=True, verbose_name="操作时间")
    
    def __str__(self):
        return f"{self.get_record_type_display()} {self.amount}"
    
    class Meta:
        db_table = 'FinancialRecord'

class TaskRecord(models.Model):
    TASK_TYPES = (
        ('inbound', '入库'),
        ('outbound', '出库'),
        ('inventory', '盘点'),
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="操作员")
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name="商品")
    task_type = models.CharField(max_length=10, choices=TASK_TYPES, verbose_name="任务类型")
    quantity = models.IntegerField(verbose_name="数量")
    operation_time = models.DateTimeField(auto_now_add=True, verbose_name="操作时间")
    
    def __str__(self):
        return f"{self.user.username} {self.get_task_type_display()} {self.goods.name} {self.quantity}"
    
    class Meta:
        db_table = 'TaskRecord'

class WageRule(models.Model):
    TASK_TYPES = (
        ('inbound', '入库'),
        ('outbound', '出库'),
        ('inventory', '盘点'),
    )
    task_type = models.CharField(max_length=10, choices=TASK_TYPES, verbose_name="任务类型")
    unit_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="单价")
    effective_from = models.DateField(verbose_name="生效开始日期")
    effective_to = models.DateField(null=True, blank=True, verbose_name="生效结束日期")
    
    def __str__(self):
        return f"{self.get_task_type_display()} 单价: {self.unit_price}"
    
    class Meta:
        db_table = 'WageRule'